package logic

import (
	"context"
	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"
	"doctor-medicine-system/models/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type MedSupplyAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedSupplyAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedSupplyAddLogic {
	return &MedSupplyAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 药品信息录入
func (l *MedSupplyAddLogic) MedSupplyAdd(in *doctorrpc.MedSupplyAddRequest) (*doctorrpc.MedSupplyAddResponse, error) {
	// todo: add your logic here and delete this line

	var doctor mysql.Doctor
	if err := doctor.FIndDoctorById(mysql.DB, in.DoctorId); err != nil {
		return nil, errors.New("该医生不存在")
	}

	medSupply := &mysql.MedSupply{
		DoctorId:         in.DoctorId,
		MedSupplyName:    in.MedSupplyName,
		MedSupplyType:    in.MedSupplyType,
		MedSupplyAddress: in.MedSupplyAddress,
		MedSupplyComment: in.MedSupplyComment,
	}
	if err := medSupply.MedSupplyAdd(mysql.DB); err != nil {
		return nil, err
	}
	return &doctorrpc.MedSupplyAddResponse{
		MedSupplyId: int64(medSupply.ID),
	}, nil
}
